﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;
using Entities;

namespace QLNhaSach.Modules.ThongTin
{
    public partial class DanhSachKhachHang : UserControl
    {
        DataTable table = new DataTable();
        public DanhSachKhachHang()
        {
            InitializeComponent();
            table.Columns.Add("STT");
            table.Columns.Add("Mã khách hàng");
            table.Columns.Add("Tên khách hàng");
            table.Columns.Add("Địa chỉ");
            table.Columns.Add("Điện thoại");
            table.Columns.Add("Email");
            table.Columns.Add("Tiền nợ");
            KhachHangBO khbo = new KhachHangBO();
            DataTable dt = khbo.layDanhSachKhachHang();

            int i = 1;
            foreach (DataRow dr in dt.Rows)
            {
                table.Rows.Add(i++, dr["KH_MAKHACHHANG"].ToString(), dr["KH_HOTENKHACHHANG"].ToString(), dr["KH_DIACHI"].ToString(), dr["KH_DIENTHOAI"].ToString(), dr["KH_EMAIL"].ToString(), dr["KH_TIENNO"].ToString());
            }
            gridControl1.DataSource = table;
            gridView1.Columns["Mã khách hàng"].Visible = false;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            
        }

        private void simpleButton7_Click(object sender, EventArgs e)
        {
            Environment.Exit(1);
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {

        }

        private void simpleButton7_Click_1(object sender, EventArgs e)
        {
            Environment.Exit(1);
        }

        private void gridView1_FocusedRowChanged_1(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow cell = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (cell != null)
            {
                String value = cell[1].ToString();

                KhachHang kh = new KhachHang();
                KhachHangBO khbo = new KhachHangBO();
                kh = khbo.layThongTinKhachHang(int.Parse(value));
                textEdit2.Text = kh.MaKhachHang.ToString();
                textEdit5.Text = kh.HoTenKhachHang.ToString();
                textEdit6.Text = kh.DiaChi.ToString();
                textEdit1.Text = kh.DienThoai.ToString();
                textEdit3.Text = kh.Email.ToString();
                textEdit4.Text = kh.TienNo.ToString();
            }
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            string maKhachHang = textEdit2.Text;
            string tenKhachHang = textEdit5.Text;
            string diaChi = textEdit6.Text;
            string dienThoai = textEdit1.Text;
            string email = textEdit3.Text;
            string tienNo = textEdit4.Text;
            KhachHangBO khbo = new KhachHangBO();
            KhachHang kh = new KhachHang();
            kh.HoTenKhachHang = tenKhachHang;
            kh.DiaChi = diaChi;
            kh.DienThoai = dienThoai;
            kh.Email = email;
            kh.TienNo = int.Parse(tienNo.ToString());
            kh.MaKhachHang = int.Parse(maKhachHang.ToString());
            int kq = khbo.UpdateKhachHang(kh);
            if (kq == 1)
            {
                MessageBox.Show("Cập nhật thành công");
                gridControl1.DataSource = null;
                DataTable dt = new DataTable();
                dt.Columns.Add("STT");
                dt.Columns.Add("Mã khách hàng");
                dt.Columns.Add("Tên khách hàng");
                dt.Columns.Add("Địa chỉ");
                dt.Columns.Add("Điện thoại");
                dt.Columns.Add("Email");
                dt.Columns.Add("Tiền nợ");
                DataTable dskh = khbo.layDanhSachKhachHang();

                int i = 1;
                foreach (DataRow dr in dskh.Rows)
                {
                    dt.Rows.Add(i++, dr["KH_MAKHACHHANG"].ToString(), dr["KH_HOTENKHACHHANG"].ToString(), dr["KH_DIACHI"].ToString(), dr["KH_DIENTHOAI"].ToString(), dr["KH_EMAIL"].ToString(), dr["KH_TIENNO"].ToString());
                }
                gridControl1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("Cập nhật thất bại");
            }
        }

        private void simpleButton2_Click_1(object sender, EventArgs e)
        {
            DataRow cell = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            String value = cell[1].ToString();
            KhachHangBO khbo = new KhachHangBO();
            int kq = khbo.DeleteKhachHang(value.ToString());
            if (kq == 1)
            {
                MessageBox.Show("Xoá khách hàng thành công");
                gridControl1.DataSource = null;
                DataTable dt = new DataTable();
                dt.Columns.Add("STT");
                dt.Columns.Add("Mã khách hàng");
                dt.Columns.Add("Tên khách hàng");
                dt.Columns.Add("Địa chỉ");
                dt.Columns.Add("Điện thoại");
                dt.Columns.Add("Email");
                dt.Columns.Add("Tiền nợ");
                DataTable dss = khbo.layDanhSachKhachHang();
                int i = 0;
                foreach (DataRow dr in dss.Rows)
                {
                    dt.Rows.Add(i++, dr["KH_MAKHACHHANG"].ToString(), dr["KH_HOTENKHACHHANG"].ToString(), dr["KH_DIACHI"].ToString(), dr["KH_DIENTHOAI"].ToString(), dr["KH_EMAIL"].ToString(), dr["KH_TIENNO"].ToString());
                }
                gridControl1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("Không thể xoá do có tham chiếu");
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            gridControl1.DataSource = null;
            DataTable dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("Mã khách hàng");
            dt.Columns.Add("Tên khách hàng");
            dt.Columns.Add("Địa chỉ");
            dt.Columns.Add("Điện thoại");
            dt.Columns.Add("Email");
            dt.Columns.Add("Tiền nợ");
            KhachHangBO khbo = new KhachHangBO();
            DataTable ds = khbo.layDanhSachKhachHang();

            int i = 1;
            foreach (DataRow dr in ds.Rows)
            {
                dt.Rows.Add(i++, dr["KH_MAKHACHHANG"].ToString(), dr["KH_HOTENKHACHHANG"].ToString(), dr["KH_DIACHI"].ToString(), dr["KH_DIENTHOAI"].ToString(), dr["KH_EMAIL"].ToString(), dr["KH_TIENNO"].ToString());
            }
            gridControl1.DataSource = dt;
        }
    }
}
